package com.funo.wcity.app.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.funo.wcity.app.dao.Page;
import com.funo.wcity.app.dao.TblCompanyTypesDAO;
import com.funo.wcity.app.dao.TblMailDetailDAO;
import com.funo.wcity.app.entity.TblCompanyTypes;
import com.funo.wcity.app.entity.TblMailDetail;
import com.funo.wcity.app.service.TblMailDetailService;
import com.funo.wcity.app.utils.StringUtil;

@Service("tblMailDetailService")
public class TblMailDetailServiceImpl implements TblMailDetailService {
    @Resource(name="tblMailDetailDAO")
	private TblMailDetailDAO tblMailDetailDAO;
    @Resource(name="tblCompanyTypesDAO")
    private TblCompanyTypesDAO tblCompanyTypesDAO;
   

	public Page getTbLMailDetailList(Integer pageNo, Integer pageSize,TblMailDetail tblMailDetail,String queryStr) {
		Page page=this.tblMailDetailDAO.getTbLMailDetailList(pageNo, pageSize, tblMailDetail,queryStr);
		List dataList=(List)page.getData();
		List resultList=new ArrayList();
		for(int i=0;i<dataList.size();i++){
			Map obj=(Map)dataList.get(i);
			//查询公司类型
			if(!StringUtil.isEmpty(obj.get("idCompanyType"))){
				String idcompanytype=(String)obj.get("idCompanyType");
				idcompanytype=idcompanytype.substring(1, idcompanytype.length()-1);
				List<TblCompanyTypes> companytypeList=this.tblCompanyTypesDAO.findTblCompaniesList(idcompanytype);
				StringBuffer sb=new StringBuffer();
				for(TblCompanyTypes tblCompanyTypes:companytypeList){
					sb.append(tblCompanyTypes.getCompanyTypeName()).append("；");
				}
				if(sb.length()>0){
					obj.put("companyTypeName", sb.substring(0, sb.length()-1));
				}
			}
			resultList.add(obj);
		}
		page.setData(resultList);
		return page;
	}	
	public void saveMailDetail(TblMailDetail tblMailDetail) {
		this.tblMailDetailDAO.saveOrUpdate(tblMailDetail);
	}

	public TblMailDetail getTblMailDetail(Long idMailDetail) {
		return this.tblMailDetailDAO.get(idMailDetail);
	}

	
	public void updateTbLMailDetail(TblMailDetail tblMailDetail,
			String idMailDetail) {
		this.tblMailDetailDAO.updateTbLMailDetail(tblMailDetail, idMailDetail); 
	}

	public Page getTbLMailDetailListInfo(Integer pageNo, Integer pageSize, TblMailDetail tblMailDetail,String queryStr) {
		Page page=this.tblMailDetailDAO.getTbLMailDetailList(pageNo, pageSize, tblMailDetail,queryStr);
		List list=(List)page.getResult();
		List dataList=new ArrayList();
		for(int i=0;i<list.size();i++){
			Map map=(Map)list.get(i);
			if(tblMailDetail.getIdMailDetail()==Long.valueOf(String.valueOf(map.get("IDMAILDETAIL")))){
				dataList.add(map);
				break;
			}
		}
		page.setResult(dataList);
		return page;
	}	
}
